/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package paymentgateway;

/**
 *
 * @author dhaval
 */
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


/**
 *
 * @author dhaval
 */
public class PaymentGatewayServer extends ClientServer {

    private ServerSocket port;
    private Socket socket;
    //   private int ccInt; // Integer value of the Credit Card Number (converted)
    private boolean flag = true;
    private String a = "";

    /**
     * PaymentGatewayServer() constructor creates a server object given it port
     * number and a number representing the number of clients it can backlog.
     *
     * @param portNum -- an int giving the port number
     * @param nBacklog -- the number of clients that can backlog
     */
    public PaymentGatewayServer(int portNum, int nBacklog) {
        try {
            port = new ServerSocket(portNum, nBacklog);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * run() defines the server thread's main behavior which is simply to
     * provide service whenever it is requested by a client. Since an I/O
     * exception may result, it is handled here. The server repeatedly accepts a
     * connection from a client and provides it whatever service is defined in
     * provideService().
     */
    public void run() {
        try {
            System.out.println("Echo server at "
                    + InetAddress.getLocalHost() + " waiting for connections ");
            while (true) {
                socket = port.accept();
                System.out.println("Accepted a connection from " + socket.getInetAddress());
                provideService(socket);
                socket.close();
                System.out.println("Closed the connection\n");
                
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } // run()

    protected void provideService(Socket socket) throws IOException {
        String data;
        String[] splitData;
        try {
            writeToSocket(socket, "Hello, Welcome to the Payment Gateway\r"
                    + "Enter Cardholdername:Cardnumber:amount :");

            data = readFromSocket(socket);
            splitData = data.split(":");

            ValidateCC validateCC = new ValidateCC();
           //System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"+splitData[1]);
            validateCC.setCardHolderName(splitData[0]); //calls the setCardHolderName() method from the PaymentGateway
            validateCC.setCreditCard(splitData[1]); //calls the setCardHolderName() method from the PaymentGateway
            validateCC.setChargeAmount(Double.parseDouble(splitData[2]));
            String message = validateCC.validate();
            writeToSocket(socket, message); // validateCC.validate returns the result of the validation

        } catch (NumberFormatException e) {
            System.out.println("Number format Error!!!" + e.getMessage());
        }

    }// provideServer()

    public static void main(String args[]) {
        PaymentGatewayServer server = new PaymentGatewayServer(10001, 3);
        server.start();
    }
}

